/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.engine;

import com.android1.amarena2d.commons.collections.FixedSizeArray;
import com.android1.amarena2d.input.InputListener;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;

public class AmarenaInputProcessor extends EngineObject implements InputProcessor {

    private final FixedSizeArray<InputListener> listener;

    public AmarenaInputProcessor(Input input) {
        this.listener = new FixedSizeArray<InputListener>(engine.getConfig().get(Config.LISTENER_MAX));
        input.setInputProcessor(this);
    }

    public void addListener(InputListener inputListener) {
        listener.add(inputListener);
    }

    public void removeInputListener(InputListener inputListener) {
        listener.remove(inputListener);
    }

    @Override
    public boolean keyDown(int keycode) {
        final int c = listener.getCount();
        if (c > 0) {
            for (int i = 0; i < c; i++) {
                if (listener.get(i).keyDown(keycode))
                    return true;
            }
        }
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        final int c = listener.getCount();
        if (c > 0) {
            for (int i = 0; i < c; i++) {
                if (listener.get(i).keyUp(keycode))
                    return true;
            }
        }
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        final int c = listener.getCount();
        if (c > 0) {
            for (int i = 0; i < c; i++) {
                if (listener.get(i).keyTyped(character))
                    return true;
            }
        }
        return false;
    }

    @Override
    public boolean touchDown(int x, int y, int pointer) {
        final int c = listener.getCount();
        if (c > 0) {
            for (int i = 0; i < c; i++) {
                if (listener.get(i).touchDown(x, y, pointer))
                    return true;
            }
        }
        return false;
    }

    @Override
    public boolean touchUp(int x, int y, int pointer) {
        final int c = listener.getCount();
        if (c > 0) {
            for (int i = 0; i < c; i++) {
                if (listener.get(i).touchUp(x, y, pointer))
                    return true;
            }
        }
        return false;
    }

    @Override
    public boolean touchDragged(int x, int y, int pointer) {
        final int c = listener.getCount();
        if (c > 0) {
            for (int i = 0; i < c; i++) {
                if (listener.get(i).touchDragged(x, y, pointer))
                    return true;
            }
        }
        return false;
    }
}
